package org.opsli.modulars.shop;

import cn.hutool.core.date.DateUtil;
import com.aliyuncs.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.opsli.api.base.result.ResultWrapper;
import org.opsli.api.web.shop.device.SysShopDeviceRestApi;

import org.opsli.api.web.shop.userorder.SysUserOrderRestApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Api(tags = "微信小程序-门店设备")
@Slf4j
@RestController
@RequestMapping("/weMini/NotToken/shopDevice")
public class WeChatShopDeviceRestController {


    @Autowired
    SysShopDeviceRestApi sysShopDeviceRestApi;
    @Autowired
    SysUserOrderRestApi sysUserOrderRestApi;
    /**
     * 最近店铺
     * @return ResultWrapper
     */
    @GetMapping("/getList")
    @ApiOperation(value = "根据日期门店id获取设备列表", notes = "根据日期 门店id获取设备列表")
    public ResultWrapper<?> getList(String orgIds,
                                    String date, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest request) {
        // 如果系统内部调用 则直接查数据库
        Date date1 = null;
        try {
            date1 = DateUtil.parse(date);
        } catch (Exception e) {
            return ResultWrapper.getCustomResultWrapper(500,"日期格式错误");
        }

        return sysShopDeviceRestApi.findPageWeChat(orgIds,date1, pageNo, pageSize,request);
    }



    /**
     * 最近店铺
     * @return ResultWrapper
     */
    @GetMapping("/openDoor")
    @ApiOperation(value = "根据日期门店id获取设备列表", notes = "根据日期 门店id获取设备列表")
    public ResultWrapper<?> openDoor(String type ,String id) {

        //如果没传订单id就查数据库内最近一次已支付的订单
        if (StringUtils.isEmpty(id)){
            id = sysUserOrderRestApi.getOpenDoorOrder().getData().getId();
        }

        // 如果系统内部调用 则直接查数据库
        if (StringUtils.isEmpty(id)&&StringUtils.isEmpty(type)){
            return ResultWrapper.getCustomResultWrapper(500,"缺少订单id或设备类型");
        }
        //判断订单是否是 已支付、已预约 消费中
        sysUserOrderRestApi.isPayOrReservation(id);


        return sysShopDeviceRestApi.openDoor(type,id);
    }
}
